<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
use app\admin\model\Networks;
class Repor extends Common{
	public function _initialize()
    {
        parent::_initialize();
        $this->Networks = new Networks;
    }
    public function index($key=null,$isReport=null){
        $params = $this->request->post();
        $params['isReport'] = 1;
    	if($this->request->isPost() || $isReport==1){
            $map = [];
            if(!empty($key)){
                $map['u.username|u.company|m.name'] = array("like","%".$key."%");
            }
            $model = $this->Networks->alias('m')
                      ->join('users u',"u.admin_id=m.user_id")
                      ->field('m.*,m.appro_time as approtime,m.status as statuslabel,u.username,u.company,m.type as typelabel')
                      ->where($map)
                      ->order('m.id desc');
                    if($isReport == 1){  //数据要导出来
                        $list = $model->select();
                        $lists['data'] = $list;
                        $lists['total'] = count($list);
                    }  else{
                         $lists =  $model->paginate(array('list_rows'=>$this->pageSize,'page'=>$page))->toArray();
                    }
                $data = [];
                $base = [];
                if($lists['data']){
                    foreach($lists['data'] as $vo){
                        if($vo['type'] == 3){
                            $vo['area'] = getCloudName($vo['local_cloud_id']);
                            $vo['address'] = getCloudName($vo['local_cloud_addrid']);
                        }else{
                            $vo['area'] = getPOPName($vo['local_cloud_id']);
                            $vo['address'] = $vo['access_addr'];
                        }
                        $vo['rearea'] = getCloudName($vo['endcloudid']);
                        $vo['daylength'] = $vo['daylength'].lang(' 个月');
                        $vo['broadband'] = $vo['broadband'].lang(' MB');
                        $vo['readdress'] =getCloudName($vo['endcloudaddrid']);
                        $base[] = array(
                            'Item' => $vo['id'],
                            'username'=>$vo['username'],
                            'company'=>$vo['company'],
                            'name'=>$vo['name'],
                            'type'=>$vo['typelabel'],
                            'area'=>$vo['area'],
                            'address'=>$vo['address'],
                            'rearea'=>$vo['rearea'],
                            'readdress'=>$vo['readdress'],
                            'broadband'=>$vo['broadband'],
                            'daylength'=>$vo['daylength'],
                        );
                        $data[] = $vo;
                    }
                }
            if($isReport){
                $this->exports($base);exit;
            }   
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$data,'count'=>$lists['total'],'rel'=>1,'repotUrl'=>U('index',$params)];
        }
        $this->assign('repotUrl',U('index',$params));
    	return $this->fetch();
    }
    
}